/*
 * @lc app=leetcode.cn id=868 lang=javascript
 *
 * [868] 二进制间距
 */

// @lc code=start
/**
 * @param {number} n
 * @return {number}
 */
var binaryGap = function (n) {
  let sign = 0, max = 1, cur = 1;
  while (n) {
    if (n % 2 == 0) {
      sign && cur++;
    } else {
      sign++;
      max = Math.max(max, cur);
      cur = 1;
    }
    n >>= 1;
  }
  return sign > 1 ? max : 0;
};
// @lc code=end

